package com.lechi.web.service.impl;

import com.lechi.web.common.enums.ErrorCodeEnum;
import com.lechi.web.common.exception.ServiceException;
import com.lechi.web.framework.log.IotExceptionLog;
import com.lechi.web.service.IIotSourceFileService;
import com.lechi.web.system.domain.IotSourceFile;
import com.lechi.web.system.mapper.iot.IotSourceFileMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.concurrent.CompletableFuture;

/**
 * 升级包管理Service业务层处理
 *
 * @author yangguang
 * @date 2024 -07-10
 */
@Service
@RequiredArgsConstructor
public class IotSourceFileServiceImpl implements IIotSourceFileService {

    private final IotSourceFileMapper iotSourceFileMapper;

    /**
     * 查询升级包管理
     *
     * @param id 升级包管理主键
     * @return 升级包管理
     */
    @Override
    public IotSourceFile selectIotSourceFileById(Long id) {
        try {
            return iotSourceFileMapper.selectIotSourceFileById(id);
        } catch (Exception e) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
            String method = Thread.currentThread().getName() + ":" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName();
            CompletableFuture.runAsync(IotExceptionLog.exceptionLog(e, "查询升级包管理", method, id));
            throw new ServiceException(ErrorCodeEnum.QUERY_ERROR.getValue(), ErrorCodeEnum.QUERY_ERROR.getKey());
        }
    }

    /**
     * 查询升级包管理列表
     *
     * @param iotSourceFile 升级包管理
     * @return 升级包管理
     */
    @Override
    public List<IotSourceFile> selectIotSourceFileList(IotSourceFile iotSourceFile) {
        try {
            return iotSourceFileMapper.selectIotSourceFileList(iotSourceFile);
        } catch (Exception e) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
            String method = Thread.currentThread().getName() + ":" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName();
            CompletableFuture.runAsync(IotExceptionLog.exceptionLog(e, "查询升级包管理列表", method, iotSourceFile));
            throw new ServiceException(ErrorCodeEnum.QUERY_ERROR.getValue(), ErrorCodeEnum.QUERY_ERROR.getKey());
        }
    }

    /**
     * 查询快照列表列表
     *
     * @param iotSourceFile 快照列表
     * @return 快照列表
     */
    @Override
    public List<IotSourceFile> selectIotScreenshotFileList(IotSourceFile iotSourceFile) {
        try {
            return iotSourceFileMapper.selectIotScreenshotFileList(iotSourceFile);
        } catch (Exception e) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
            String method = Thread.currentThread().getName() + ":" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName();
            CompletableFuture.runAsync(IotExceptionLog.exceptionLog(e, "查询快照列表", method, iotSourceFile));
            throw new ServiceException(ErrorCodeEnum.QUERY_ERROR.getValue(), ErrorCodeEnum.QUERY_ERROR.getKey());
        }
    }

    /**
     * 查询文档列表
     *
     * @param iotSourceFile 文档列表
     * @return 文档列表
     */
    @Override
    public List<IotSourceFile> selectIotDocFileList(IotSourceFile iotSourceFile) {
        try {
            return iotSourceFileMapper.selectIotDocFileList(iotSourceFile);
        } catch (Exception e) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
            String method = Thread.currentThread().getName() + ":" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName();
            CompletableFuture.runAsync(IotExceptionLog.exceptionLog(e, "查询文档列表", method, iotSourceFile));
            throw new ServiceException(ErrorCodeEnum.QUERY_ERROR.getValue(), ErrorCodeEnum.QUERY_ERROR.getKey());
        }
    }

    /**
     * 查询升级包管理列表
     *
     * @param iotSourceFile 升级包管理
     * @return 升级包管理
     */
    @Override
    public List<IotSourceFile> selectIotApkFileList(IotSourceFile iotSourceFile) {
        try {
            return iotSourceFileMapper.selectIotApkFileList(iotSourceFile);
        } catch (Exception e) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
            String method = Thread.currentThread().getName() + ":" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName();
            CompletableFuture.runAsync(IotExceptionLog.exceptionLog(e, "查询APK列表", method, iotSourceFile));
            throw new ServiceException(ErrorCodeEnum.QUERY_ERROR.getValue(), ErrorCodeEnum.QUERY_ERROR.getKey());
        }
    }

    /**
     * 新增升级包管理
     *
     * @param iotSourceFile 升级包管理
     */
    @Override
    public void insertIotSourceFile(IotSourceFile iotSourceFile) {
        iotSourceFile.setCreateTime(new Date());
        try {
            iotSourceFileMapper.insertIotSourceFile(iotSourceFile);
        } catch (Exception e) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
            String method = Thread.currentThread().getName() + ":" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName();
            CompletableFuture.runAsync(IotExceptionLog.exceptionLog(e, "新增升级包管理", method, iotSourceFile));
            throw new ServiceException(ErrorCodeEnum.ADD_ERROR.getValue(), ErrorCodeEnum.ADD_ERROR.getKey());
        }
    }

    /**
     * 修改升级包管理
     *
     * @param iotSourceFile 升级包管理
     */
    @Override
    public void updateIotSourceFile(IotSourceFile iotSourceFile) {
        try {
            iotSourceFileMapper.updateIotSourceFile(iotSourceFile);
        } catch (Exception e) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
            String method = Thread.currentThread().getName() + ":" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName();
            CompletableFuture.runAsync(IotExceptionLog.exceptionLog(e, "修改升级包管理", method, iotSourceFile));
            throw new ServiceException(ErrorCodeEnum.EDIT_ERROR.getValue(), ErrorCodeEnum.EDIT_ERROR.getKey());
        }
    }

    /**
     * 批量删除升级包管理
     *
     * @param ids 需要删除的升级包管理主键
     * @return 结果
     */
    @Override
    public int deleteIotSourceFileByIds(Long[] ids) {
        try {
            return iotSourceFileMapper.deleteIotSourceFileByIds(ids);
        } catch (Exception e) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
            String method = Thread.currentThread().getName() + ":" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName();
            CompletableFuture.runAsync(IotExceptionLog.exceptionLog(e, "批量删除升级包管理", method, ids));
            throw new ServiceException(ErrorCodeEnum.EDIT_ERROR.getValue(), ErrorCodeEnum.EDIT_ERROR.getKey());
        }
    }

}
